package com.example.oauth2.domain;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.oauth2.generator.domain.UserTable;
import com.example.oauth2.generator.mapper.UserTableMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;

/**
 * @ClassName JiuBoDouUserDetailService
 * @Description
 * @Author huyingliang
 * @Date 2024-03-05 10:52
 */
@Service
public class JiuBoDouUserDetailService implements UserDetailsService {

    @Autowired
    private UserTableMapper userTableMapper;

    @Override
    public UserDetails loadUserByUsername(String userMobile) throws UsernameNotFoundException {
        ArrayList<SimpleGrantedAuthority> simpleGrantedAuthorities = new ArrayList<>();
        simpleGrantedAuthorities.add(new SimpleGrantedAuthority("admin"));

        LambdaQueryWrapper<UserTable> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserTable::getUserMobile, userMobile);
        UserTable userTable = userTableMapper.selectOne(queryWrapper);
        JiuBoDouUserDetails jiuBoDouUserDetails = new JiuBoDouUserDetails(userTable.getUserMobile()
                , userTable.getUserPassword(), simpleGrantedAuthorities);

        return jiuBoDouUserDetails;
    }
}
